<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="innerCode">
        <el-input v-model="queryParams.innerCode" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:machine:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:machine:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:machine:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:machine:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="machineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备编号" align="center" prop="innerCode" />
      <el-table-column label="设备型号" align="center" prop="vmTypeId">
        <template #default="scope">
          <div v-for="item in vmTypeList" :key="item.id">
            <span v-if="item.id == scope.row.vmTypeId">{{ item.name }}</span>

          </div>

        </template>

      </el-table-column>

      <el-table-column label="详细地址" align="center" prop="addr" />
      <el-table-column label="合作商" align="center" prop="partnerId">
        <template #default="scope">
          <div v-for="item in partnerList" :key="item.id">
            <span v-if="item.id == scope.row.partnerId">{{ item.partnerName }}</span>

          </div>

        </template>

      </el-table-column>

      <el-table-column label="设备状态" align="center" prop="vmStatus">
        <template #default="scope">
          <dict-tag :options="vm_status" :value="scope.row.vmStatus" />
        </template>

      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleGoods(scope.row)" v-hasPermi="['manage:vm:edit']">货道</el-button>
          <el-button link type="primary" @click="handleUpdatePolicy(scope.row)" v-hasPermi="['manage:vm:edit']">策略</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:machine:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:machine:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-show="total>0"
      :total="total"
      @current-change="currentchange"
      
      v-model="queryParams.pageNum"
      :page-size="queryParams.pageSize"

    />

    <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" /> -->

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号">
          <span>{{ form.innerCode == null ? '系统自动生成' : form.innerCode }}</span>

        </el-form-item>

        <el-form-item label="供货时间" v-if="form.innerCode != null">
          <span>{{ parseTime(form.lastSupplyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>

        </el-form-item>

        <el-form-item label="设备类型" v-if="form.innerCode != null">
          <div v-for="item in vmTypeList" :key="item.id">
            <span v-if="form.vmTypeId == item.id">{{ item.name }}</span>

          </div>

        </el-form-item>

        <el-form-item label="设备容量" v-if="form.innerCode != null">
          <span>{{ form.channelMaxCapacity }}</span>

        </el-form-item>

        <el-form-item label="选择型号" prop="vmTypeId" v-if="form.innerCode == null">
          <!-- <el-input v-model="form.vmTypeId" placeholder="请输入设备型号" /> -->
          <el-select v-model="form.vmTypeId" placeholder="请选择设备型号" style="width: 100%">
            <el-option v-for="item in vmTypeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>

        </el-form-item>

        <el-form-item label="选择点位" prop="nodeId">
          <!-- <el-input v-model="form.nodeId" placeholder="请输入点位Id" /> -->
          <el-select v-model="form.nodeId" placeholder="请选择点位" style="width: 100%">
            <el-option v-for="item in nodeList" :key="item.id" :label="item.nodeName" :value="item.id" />
          </el-select>

        </el-form-item>

        <el-form-item label="合作商" v-if="form.innerCode != null">
          <div v-for="item in partnerList" :key="item.id">
            <span v-if="form.partnerId == item.id">{{ item.partnerName }}</span>

          </div>

        </el-form-item>

        <el-form-item label="所属区域" v-if="form.innerCode != null">
          <div v-for="item in regionList" :key="item.id">
            <span v-if="form.regionId == item.id">{{ item.regionName }}</span>

          </div>

        </el-form-item>

        <el-form-item label="设备地址" v-if="form.innerCode != null">
          <span>{{ form.addr }}</span>

        </el-form-item>




      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 策略管理对话框 -->
<el-dialog title="策略管理" v-model="policyOpen" width="500px" append-to-body>
  <el-form ref="vmRef" :model="form1" label-width="80px">
    <el-form-item label="策略" prop="policyId">
      <el-select v-model="form1.policyId" placeholder="请选择策略">
        <el-option v-for="item in policyList" :key="item.policyId" :label="item.policyName"
          :value="item.policyId"></el-option>

      </el-select>

    </el-form-item>

  </el-form>

  <template #footer>
    <div class="dialog-footer">
      <el-button type="primary" @click="submitForm1">确 定</el-button>

      <el-button @click="cancel1">取 消</el-button>

    </div>

  </template>

</el-dialog>
<ChannelDialog :goodVisible="goodVisible" :goodData="goodData" @handleCloseGood="handleCloseGood"></ChannelDialog>

  </div>
</template>
<script setup>
const { proxy } = getCurrentInstance();
const { vm_status } = proxy.useDict("vm_status");
</script>
<style lang="scss" scoped src="./index.scss"></style>
<script>
  import ChannelDialog from './components/ChannelDialog.vue';
import { listMachine, getMachine, delMachine, addMachine, updateMachine } from "@/api/system/machine";
import { listType } from "@/api/system/vmType";
import { listPartner } from "@/api/system/partner";
import { loadAllParams } from '@/api/page';
import { listNode } from '@/api/system/node';
import { listRegion } from "@/api/system/region";
import { listPolicy } from '@/api/system/policy';
import { updatePolicy } from "../../../api/system/policy";
export default {
  name: "Machine",
  data() {
    return {
      policyOpen:false,
      policyList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      form1:{},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      machineList: [],
      partnerList: [],
      // 弹出层标题
      title: "",
      goodVisible:false,
      goodData:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        innerCode: null,
        channelMaxCapacity: null,
        nodeId: null,
        addr: null,
        lastSupplyTime: null,
        businessType: null,
        regionId: null,
        partnerId: null,
        vmTypeId: null,
        vmStatus: null,
        runningStatus: null,
        longitudes: null,
        latitude: null,
        clientId: null,
        policyId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nodeId: [
          { required: true, message: "点位Id不能为空", trigger: "blur" }
        ],
        lastSupplyTime: [
          { required: true, message: "上次补货时间不能为空", trigger: "blur" }
        ],
        businessType: [
          { required: true, message: "商圈类型不能为空", trigger: "change" }
        ],
        regionId: [
          { required: true, message: "区域Id不能为空", trigger: "blur" }
        ],
        partnerId: [
          { required: true, message: "合作商Id不能为空", trigger: "blur" }
        ],
        vmTypeId: [
          { required: true, message: "设备型号不能为空", trigger: "blur" }
        ],
        vmStatus: [
          { required: true, message: "设备状态，0:未投放;1-运营;3-撤机不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      },
      vmTypeList: [],
      partnerList: [],
      nodeList: [],
      regionList: [],
    };
  },
  created() {
    this.getList();
    this.getVmTypeList()
    this.getNodeList()
    this.getPartnerList()
    this.getRegionList()
  },
  methods: {
    handleGoods(row){
      this.goodVisible = true;
        this.goodData = row;
    },
    // 关闭货道弹层
     handleCloseGood () {
        this.goodVisible = false;
    },
    handleUpdatePolicy(row){
      this.policyOpen=true;
      this.form1.nodeId=row.nodeId;
      this.form1.id=row.id;
      this.form1.policyId=row.policyId;
      this.queryParams.pageNum=1;
      this.queryParams.pageSize=1000000;

      listPolicy(this.queryParams).then(res=>{
        console.log(res)
        this.policyList=res.rows

      })
    },
    submitForm1(){
      updateMachine(this.form1).then(res=>{
        console.log(res);
        this.policyOpen=false
        this.getList()
      })
    },
    cancel1(){
      this.policyOpen=false;
    },
    currentchange(e){
      this.queryParams.pageNum=e
      this.getList()
      console.log(e)
    },
    getVmTypeList() {
      listType(loadAllParams).then((response) => {
        this.vmTypeList = response.rows;
      });
    },

    getPartnerList() {
      listPartner(loadAllParams).then((response) => {
        this.partnerList = response.rows;
      });
    },

    getNodeList() {
      listNode(loadAllParams).then((response) => {
        this.nodeList = response.rows;
      });
    },

    getRegionList() {
      listRegion(loadAllParams).then((response) => {
        this.regionList = response.rows;
      });
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listMachine(this.queryParams).then(response => {
        this.machineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        innerCode: null,
        channelMaxCapacity: null,
        nodeId: null,
        addr: null,
        lastSupplyTime: null,
        businessType: null,
        regionId: null,
        partnerId: null,
        vmTypeId: null,
        vmStatus: null,
        runningStatus: null,
        longitudes: null,
        latitude: null,
        clientId: null,
        policyId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMachine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMachine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMachine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + ids + '"的数据项？').then(function () {
        return delMachine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/machine/export', {
        ...this.queryParams
      }, `machine_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
